import { defineStore } from 'pinia'
import { loginPage } from '@/config'
import type { GetInfo } from '@/types/api/user'

export const useUserStore = defineStore('user', () => {
	// 用户信息
	const userInfo = ref<GetInfo.Body | null>()
	const token = ref()
	
	// 双token系统
	const accessToken = ref('')
	const refreshToken = ref('')
	const accessTokenExpires = ref(0)
	const refreshTokenExpires = ref(0)

	const setToken = (val : string) => {
		token.value = val
	}

	const setUserInfo = (val : GetInfo.Body | null) => {
		userInfo.value = val
	}
	
	// 设置访问token
	const setAccessToken = (token: string, expiresIn: number = 15 * 60) => {
		accessToken.value = token
		accessTokenExpires.value = Date.now() + expiresIn * 1000
		// 同时设置到本地存储
		uni.setStorageSync('accessToken', token)
		uni.setStorageSync('accessTokenExpires', accessTokenExpires.value)
	}
	
	// 设置刷新token
	const setRefreshToken = (token: string, expiresIn: number = 7 * 24 * 60 * 60) => {
		refreshToken.value = token
		refreshTokenExpires.value = Date.now() + expiresIn * 1000
		// 同时设置到本地存储
		uni.setStorageSync('refreshToken', token)
		uni.setStorageSync('refreshTokenExpires', refreshTokenExpires.value)
	}
	
	// 检查访问token是否过期
	const isAccessTokenExpired = () => {
		return !accessToken.value || Date.now() >= accessTokenExpires.value
	}
	
	// 检查刷新token是否过期
	const isRefreshTokenExpired = () => {
		return !refreshToken.value || Date.now() >= refreshTokenExpires.value
	}
	
	// 检查是否需要刷新token（剩余时间少于5分钟）
	const needRefreshToken = () => {
		if (!accessToken.value || !accessTokenExpires.value) return true
		const remainingTime = accessTokenExpires.value - Date.now()
		return remainingTime < 5 * 60 * 1000 // 5分钟
	}
	
	// 清除所有token
	const clearTokens = () => {
		accessToken.value = ''
		refreshToken.value = ''
		accessTokenExpires.value = 0
		refreshTokenExpires.value = 0
		uni.removeStorageSync('accessToken')
		uni.removeStorageSync('refreshToken')
		uni.removeStorageSync('accessTokenExpires')
		uni.removeStorageSync('refreshTokenExpires')
	}
	
	// 初始化token（从本地存储恢复）
	const initTokens = () => {
		const storedAccessToken = uni.getStorageSync('accessToken')
		const storedRefreshToken = uni.getStorageSync('refreshToken')
		const storedAccessExpires = uni.getStorageSync('accessTokenExpires')
		const storedRefreshExpires = uni.getStorageSync('refreshTokenExpires')
		
		if (storedAccessToken && storedAccessExpires) {
			accessToken.value = storedAccessToken
			accessTokenExpires.value = storedAccessExpires
		}
		
		if (storedRefreshToken && storedRefreshExpires) {
			refreshToken.value = storedRefreshToken
			refreshTokenExpires.value = storedRefreshExpires
		}
	}

	// 退出登录
	const logout = () => {
		setUserInfo(null)
		setToken('')
		clearTokens()

		// 当前环境地址
		const requestUrl = uni.getStorageSync('requestUrl') || ''

		uni.clearStorageSync()

		if (requestUrl) uni.setStorageSync('requestUrl', requestUrl)

		uni.reLaunch({ url: loginPage })
	}
	
	// 初始化
	initTokens()

	return { 
		userInfo, 
		token, 
		accessToken,
		refreshToken,
		accessTokenExpires,
		refreshTokenExpires,
		setToken, 
		setUserInfo,
		setAccessToken,
		setRefreshToken,
		isAccessTokenExpired,
		isRefreshTokenExpired,
		needRefreshToken,
		clearTokens,
		logout 
	}
}, {
	// 开启后数据读写都将持久化
	unistorage: true
})